<script setup lang="ts">
import Test_title from '@/components/Test_title.vue'
import { ref } from 'vue'
import { adminGetPaperListService } from '@/api/admin'
import { Reading } from '@element-plus/icons-vue'
import router from '@/router'

const test_title = ref([
  {
    id: 1,
    title: '测试1',
    publisher: '胡清钟',
    testTime: null,
    status: 1,
    beginTime: '2024-12-10',
    endTime: '2024-12-11'
  },
  {
    id: 2,
    title: '测试2',
    publisher: '胡清钟',
    testTime: 10,
    status: 3,
    beginTime: '2024-12-10',
    endTime: '2024-12-11'
  }
])

// 定义 status 枚举
enum Status {
  NotStarted = 0,
  InProgress = 1,
  Completed = 2,
  Ended = 3
}

// 计算属性，基于 status 返回标签内容和类型
const statusTag = (status: number) => {
  switch (status) {
    case Status.NotStarted:
      return { label: '未开始', type: 'info' }
    case Status.InProgress:
      return { label: '进行中', type: 'primary' }
    case Status.Completed:
      return { label: '已完成', type: 'success' }
    case Status.Ended:
      return { label: '已结束', type: 'danger' }
    default:
      return { label: '未知状态', type: 'info' }
  }
}

const formData = ref({
  currentPage: 1,
  pageSize: 10,
  searchContent: '',
  status: '全部'
})

const totalPage = ref()

const getPaperList = async () => {
  const res = await adminGetPaperListService(formData.value)
  console.log(res.data.data)
  test_title.value = res.data.data.records
  totalPage.value = res.data.data.total
}

getPaperList()

const viewStatistics = (id: number) => {
  router.push(`/teacher/test/statistics/${id}`)
}
</script>

<template>
  <div class="search-area">
    <el-input
      placeholder="请输入试卷名称"
      style="width: 250px; margin-left: 20px"
      v-model="formData.searchContent"
    ></el-input>
    <el-button type="primary" @click="getPaperList">搜索</el-button>
    <el-select style="width: 200px" v-model="formData.status" placeholder="请选择状态">
      <el-option value="全部">全部</el-option>
      <el-option value="进行中">进行中</el-option>
      <el-option value="已截止">已截止</el-option>
    </el-select>
  </div>
  <el-scrollbar style="margin-top: 20px" height="70vh">
    <Test_title v-for="item in test_title" :key="item.id">
      <template #status>
        <el-tag :type="statusTag(item.status).type">{{ statusTag(item.status).label }}</el-tag>
      </template>
      <template #title>{{ item.title }}</template>
      <template #publisher>{{ item.publisher }}</template>
      <template #testTime>{{ item.testTime !== 0 ? item.testTime + '分钟' : '不限时' }}</template>
      <template #duration>{{ item.beginTime }} 至 {{ item.endTime }}</template>
      <template #check>
        <el-icon class="delete-icon" @click="viewStatistics(item.id)" style="font-size: 30px">
          <Reading />
        </el-icon>
        <a @click="viewStatistics(item.id)">分数统计</a>
      </template>
    </Test_title>
    <el-empty v-if="test_title.length === 0" :image-size="200" />
  </el-scrollbar>
  <div class="pagination-area">
    <el-pagination
      v-model:current-page="formData.currentPage"
      background
      layout="prev, pager, next"
      :total="totalPage"
      @current-change="getPaperList"
    />
  </div>
</template>

<style scoped>
.search-area {
  margin-top: 20px;
  width: 600px;
  display: flex;
  justify-content: space-between;
}

.delete-icon:hover {
  color: skyblue;
}
</style>
